package com.yunhe.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yunhe.bean.CommodityClassify;
import com.yunhe.bean.page;
import com.yunhe.test.test;

/**
 * Servlet implementation class CommodityClassify_Servlet
 */
@WebServlet("/CommodityClassify_Servlet")
public class CommodityClassify_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommodityClassify_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//新增分类页面的登录前控制器
		if("showadd".equals(request.getParameter("manage"))){
			test te =new test();
		     List<CommodityClassify> list=te.classifycla();
		     System.out.println(list);
		       Map map2=new HashMap();
		       map2.put("ClassifyList",list);
		        //传递map到前端
				request.setAttribute("map2",map2);
				//页面转向到前端
				request.getRequestDispatcher("manage/productClass-add.jsp").forward(request, response);
		}
		//修改页面的登录前控制器
		if("showall".equals(request.getParameter("manage"))){
			String bigName= request.getParameter("big_name");
			int bigId=0;
			if(request.getParameter("big_id")!=null){
				 bigId= Integer.decode(request.getParameter("big_id"));
			}
			int smallId=0;
			if(request.getParameter("small_id")!=null){
				 smallId=Integer.decode(request.getParameter("small_id"));
			}
			String smallName= request.getParameter("small_name");
			System.out.println(bigId+""+bigName+""+smallId+""+smallName+"");
			CommodityClassify cc=new CommodityClassify();
			cc.setBig_id(bigId);
			cc.setBig_name(bigName);
			cc.setSmall_id(smallId);
			cc.setSmall_name(smallName);		
			 test te =new test();
		     List<CommodityClassify> list=te.classifycla();
		     System.out.println(list);
		       Map map1=new HashMap();
		       map1.put("ClassifyList",list);
		       map1.put("CommodityClassify", cc);
		        //传递map到前端       
				request.setAttribute("map1",map1);
				//页面转向到前端
				request.getRequestDispatcher("manage/productClass-modify.jsp").forward(request, response);
		}else{
			
		
		//后改用ajax显示分类主页面，该部分废弃
			
		test test=new test();
		//设置默认页面数为1
		int defaultPage=1;
		//判断是否是首次进入该页面,如果不是则改变默认页面数
		if(request.getParameter("currentPage")!=null){
			defaultPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		//设置一个page对象
		page pg=new page();
		pg.setCurrentPage(defaultPage);
		int countComment=test.classifyCount();
		pg.setTotalNews(countComment);
		int totalPage=pg.getTotalPage();
		//引用bus对象及方法		
		Map map=new HashMap();
		ArrayList commentList=new ArrayList();
		commentList=(ArrayList) test.classifyc(pg);
		System.out.println(commentList);
		map.put("commentList",commentList);
		map.put("totalPage", totalPage);
		map.put("page", pg);
		//传递map到前端
		request.setAttribute("map",map);
		//页面转向到前端
		request.getRequestDispatcher("manage/productClass.jsp").forward(request, response);
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
